package com.hanter.android.radlib.memo;


import android.os.Parcel;
import android.os.Parcelable;

public class PageMemoHelper implements Parcelable {

    private PageStatus status;
    private PageStorage storage;

    public PageMemoHelper() {
        status = new PageStatus();
        storage = new PageStorage(status.createMemento());
    }

    public void updateRefreshStatus(boolean pullDown) {
        save();

        if (pullDown) {
            status.pullDown();
        } else {
            status.pullUp();
        }
    }

    public PageStatus getStatus() {
        return status;
    }

    private void save() {
        storage.setMemento(status.createMemento());
    }

    public void restore() {
        status.restoreMemento(storage.getMemento());
    }

    public int getPosition() {
        return status.getPosition();
    }

    public int getMemoPosition() {
        return storage.getMemento().getPosition();
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeParcelable(this.status, flags);
        dest.writeParcelable(this.storage, flags);
    }

    protected PageMemoHelper(Parcel in) {
        this.status = in.readParcelable(PageStatus.class.getClassLoader());
        this.storage = in.readParcelable(PageStorage.class.getClassLoader());
    }

    public static final Parcelable.Creator<PageMemoHelper> CREATOR = new Parcelable.Creator<PageMemoHelper>() {
        @Override
        public PageMemoHelper createFromParcel(Parcel source) {
            return new PageMemoHelper(source);
        }

        @Override
        public PageMemoHelper[] newArray(int size) {
            return new PageMemoHelper[size];
        }
    };
}
